package com.example.photography.Activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.R;
import com.example.photography.adapter.ViewPagerAdapter;
import com.example.photography.bean.GroupBean;
import com.example.photography.bean.UrlBean;
import com.example.photography.common.BaseActivity;
import com.example.photography.fragment.group.Group_Hot;
import com.example.photography.ui.CircleImageView;
import com.example.photography.util.UIUtils;
import com.example.photography.util.Util;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GroupActivity extends BaseActivity {

    @BindView(R.id.iv_cover)
    ImageView ivCover;
    @BindView(R.id.tv_detail)
    TextView tvDetail;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_focusnums)
    TextView tvFocusnums;
    @BindView(R.id.rl_item)
    RelativeLayout rlItem;
    @BindView(R.id.tl_back)
    ImageView tlBack;
    @BindView(R.id.tl_style)
    TextView tlStyle;
    @BindView(R.id.tl_share)
    ImageView tlShare;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tt_MeItemTab)
    TabLayout ttMeItemTab;
    @BindView(R.id.appbarlayot)
    AppBarLayout appbarlayot;
    @BindView(R.id.vp_invest)
    ViewPager vpInvest;
    @BindView(R.id.ft_add)
    FloatingActionButton ftAdd;
    private GroupBean groupBean = new GroupBean();
    private ViewPagerAdapter viewPagerAdapter;
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void initData() {
        sendHttp();
        initView();
        initListen();
    }

    private void initListen() {
        tlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                removeCurrentActivity();
            }
        });

        ftAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Util.showToast(GroupActivity.this, "点击了添加");
            }
        });
    }

    private void sendHttp() {
        //模拟数据
        String content = Util.getJson(this, "group_detail.json");
//        content = Util.getJson(getActivity(),"store_home.json");

        if (TextUtils.isEmpty(content)) {//数据为空
            return;
        }
        JSONObject jsonObject = JSON.parseObject(content);
        int status = jsonObject.getInteger("code");
        if (status == 202) {
            String data = jsonObject.getString("data");
            //封装转json数据
            groupBean = JSONObject.parseObject(data, GroupBean.class);

        } else {
            // TODO: 2021/5/2 加没数据的判断
        }
    }

    private void initView() {
        tlStyle.setText("#" + groupBean.getStyle() + "#");
        tvDetail.setText(groupBean.getDetail());
        Glide.with(this)
                .load(groupBean.getHead_url())
                .thumbnail(Glide.with(this).load(R.drawable.loading))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(40)))
                .into(ivHead);
        tvName.setText(groupBean.getName());
        tvFocusnums.setText(groupBean.getPeople_num() + "已关注");
        List<UrlBean> urlBeans = JSONObject.parseArray(groupBean.getProduct(), UrlBean.class);
        Glide.with(this)
                .load(urlBeans.get(0).getUrl())
                .thumbnail(Glide.with(this).load(R.drawable.loading))
                .into(ivCover);
        initFragments();
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), UIUtils.getStringArr(R.array.group_tab), fragmentList);
        vpInvest.setAdapter(viewPagerAdapter);
        vpInvest.setCurrentItem(0);//设置viewpager的初始化位置
        ttMeItemTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //将TabLayout绑定ViewPager
        ttMeItemTab.setupWithViewPager(vpInvest);
    }

    private void initFragments() {
        //添加到集合中
        fragmentList.add(new Group_Hot());
        fragmentList.add(new Group_Hot());

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_group;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
